package com.xrz.sxm.base.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.xrz.sxm.base.BaseApplication;

public class LoadImageUtils {

	private static LoadImageUtils m_ImageUtils;

	public static LoadImageUtils newInstant() {
		if (m_ImageUtils == null)
			m_ImageUtils = new LoadImageUtils();
		return m_ImageUtils;
	}

	private Context c;

	public void displayByImageLoader(Context c, final ImageView view,
			ProgressBar pbLoading, String url, boolean isgif) {
		this.c = c;
		BaseApplication application = (BaseApplication) ((Activity) c)
				.getApplication();
		application.getImageLoader().displayImage(url, view,
				application.getDisplayImageOptions(),
				new ImageLoaderCallBack(view, pbLoading, isgif),
				new ImageLoaderCallBack(view, pbLoading, isgif));
	}

	private class ImageLoaderCallBack implements ImageLoadingListener,
			ImageLoadingProgressListener {

		ProgressBar progressBar;
		ImageView view; 

		ImageLoaderCallBack(View view, ProgressBar progressBar, boolean isgif) {
			this.progressBar = progressBar;
			this.view = (ImageView) view;
		}

		@Override
		public void onLoadingCancelled(String arg0, View arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
			view.setImageBitmap(arg2);
			progressBar.setVisibility(View.GONE);
		}

		@Override
		public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onLoadingStarted(String arg0, View arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProgressUpdate(String arg0, View arg1, int arg2, int arg3) {
			// TODO Auto-generated method stub
			float progress = (float) arg2 / (float) arg3;
			progressBar.setProgress((int) (progress * 100));
		}

	}

}
